package com.way.filemanager.ui.dialogs;

import com.way.filemanager.R;

import hwdroid.dialog.AlertDialog;
import hwdroid.dialog.DialogInterface;
import hwdroid.dialog.DialogInterface.OnCancelListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class CustomProgressDialog  {
    private CharSequence mMessage;
    private ProgressBar mProgressView;
    private TextView mMessageView;
    private TextView mProgressNumberView;

    private Handler mViewUpdateHandler;

    private String mProgressNumberFormat;
    private String mActionString;
    Context mContext;
    AlertDialog mAlertDialog;
    public CustomProgressDialog(Context context,String message) {
        mContext = context;
        mActionString = message;
        initFormats();


        AlertDialog.Builder  mBuilder = new AlertDialog.Builder(mContext);

        mAlertDialog = mBuilder.create();
        mAlertDialog.setCanceledOnTouchOutside(false);
        onCreate();
    }

    private void initFormats() {
        mProgressNumberFormat = "(%d/%d)";
    }

    public static CustomProgressDialog create(Context context, String message) {
        CustomProgressDialog dialog = new CustomProgressDialog(context,message);
//        dialog.setTitle(title);
        //dialog.setMessage(message);
        //dialog.setIcon(null);

        return dialog;
    }

    public void setMessage(CharSequence message) {
        mMessage = message;

        if(null != mMessageView) {
            mMessageView.setText(mMessage);
        }
    }

//  @Override
//  public void setTitle(CharSequence title) {
//      mTitle = title;
//
//      if(null != mTitleView) {
//          mTitleView.setText(mTitle);
//      }
//  }

    protected void onCreate() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View contentView = inflater.inflate(R.layout.custom_progress_dialog_layout, null);
        mProgressView = (ProgressBar)contentView.findViewById(R.id.progress);
        mMessageView = (TextView)contentView.findViewById(R.id.progress_message);
        mProgressNumberView = (TextView)contentView.findViewById(R.id.progress_percent);
//      mTitleView = (TextView)contentView.findViewById(R.id.progress_title);
        contentView.setMinimumWidth(10000);
        mViewUpdateHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                /* Update the number and percent */
                int progress = mProgressView.getProgress();
                int max = mProgressView.getMax();
                if(mProgressNumberFormat != null) {
                    String format = mProgressNumberFormat;
                    mProgressNumberView.setText(mActionString + String.format(format, progress, max));
                }
            }
        };

        mAlertDialog.setView(contentView);

    }


    public void setProgress(int value) {
        if(mAlertDialog.isShowing()) {
            mProgressView.setProgress(value);
            onProgressChanged();
        } else {
        }
    }

    public void setMax(int max) {
        if (mProgressView != null) {
            mProgressView.setMax(max);
            onProgressChanged();
        } else {
        }
    }

    private void onProgressChanged() {
        if(mViewUpdateHandler != null && !mViewUpdateHandler.hasMessages(0)) {
            mViewUpdateHandler.sendEmptyMessage(0);
        }
    }

    public void setButton(int buttonPositive, CharSequence text,
            hwdroid.dialog.DialogInterface.OnClickListener onClickListener) {
        // TODO Auto-generated method stub
        mAlertDialog.setButton(buttonPositive, text,onClickListener);
    }

    public void show() {
        // TODO Auto-generated method stub
        mAlertDialog.show();
    }

    public void setOnCancelListener(OnCancelListener onCancelListener) {
        // TODO Auto-generated method stub
        mAlertDialog.setOnCancelListener(onCancelListener);
    }

    public boolean isShowing() {
        // TODO Auto-generated method stub
        return mAlertDialog.isShowing();
    }

    public void dismiss() {
        // TODO Auto-generated method stub
        mAlertDialog.dismiss();
    }
}
